﻿#region Copyright Notice
// 
// 
// Copyright (c) 2009-2010 Anindya Chatterjee
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// 
// 
// 
#endregion
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Calendar.Properties;
using ComponentFactory.Krypton.Toolkit;
using Event.Plugin;
using Event.Plugin.Utility;

namespace Calendar
{
    public partial class CalendarNotifier : BaseNotifyTip
    {
        internal List<CalendarEvent> _localEvents;

        public CalendarNotifier(AbstractEvent absEvent) : base(absEvent)
        {
            InitializeComponent();
            _localEvents = new List<CalendarEvent>();
            eventLayoutPanel.VerticalScroll.Enabled = true;
            eventLayoutPanel.HorizontalScroll.Visible = false;
            eventPanel.AutoScroll = false;
        }

        public void AddEvent(CalendarEvent calEvent)
        {
            TipIcon = Resources.Calendar;
            Text = "Organizer";
            TipHeading = "Organizer Event";

            if (!_localEvents.Contains(calEvent))
            {
                _localEvents.Add(calEvent);
                DrawEvents();
            }
        }

        private void DrawEvents()
        {
            eventLayoutPanel.Controls.Clear();
            foreach (CalendarEvent localEvent in _localEvents)
            {
                var button = new KryptonButton
                                 {
                                     Text = localEvent.EventTitle,
                                     Tag = localEvent,
                                     Anchor = (AnchorStyles.Top | AnchorStyles.Left),
                                     Width = eventLayoutPanel.ClientRectangle.Width - 50
                                 };
                button.Click += ButtonClick;
                eventLayoutPanel.Controls.Add(button);

                var border = new KryptonBorderEdge
                                 {
                                     AutoSize = false,
                                     Dock = DockStyle.Top,
                                     Width = eventLayoutPanel.ClientRectangle.Width - 50
                                 };
                eventLayoutPanel.Controls.Add(border);
                border.AutoSize = true;
            }
            eventLayoutPanel.Refresh();
            eventLayoutPanel.Invalidate();
        }

        private static void ButtonClick(object sender, EventArgs e)
        {
            var calendarEvent = ((KryptonButton) sender).Tag as CalendarEvent;
            if (calendarEvent != null)
            {
                var detail = new EventDetails();
                detail.SetDescrition(calendarEvent.Description);
                detail.SetTitle(calendarEvent.EventTitle);
                detail.Show();
            }
        }

        public override void Update(BaseNotifyTip tip)
        {
            base.Update(tip);
        }
    }
}